 <template>
  <div class="countyUnit">
    <el-form label-position="right" :model="form" label-width="80px">
      <div class="maintitle">
        <div class="stepnumber">1</div>
        <div class="title">竣工测绘机构选取方式</div>
      </div>
      <div class="formcontent">
        <el-form-item label="选取方式" label-width="121px">
          <el-select
            :disabled="disabled"
            v-model="form.pickMethod"
            placeholder="请选择"
            @change="getstyles"
          >
            <el-option
              v-for="item in companyList"
              :key="item.pickMethod"
              :label="item.methodName"
              :value="item.pickMethod"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.pickMethod!=4" :label="pickFileName" label-width="121px">
          <el-upload
            class="upload-demo"
            :action="uploadUrl"
            v-if="!disabled"
            accept=".pdf"
            :disabled="disabled"
            :file-list="fileList"
            :on-success="gerFileList"
            :on-remove="removeFileList"
          >
            <el-button v-if="!disabled" size="small">上传</el-button>
            <!-- <el-button @click="seePdf(0)" v-if="disabled" size="small">查看</el-button> -->
          </el-upload>
          <seeFujianForList v-if="disabled" @openPdf="seePdf" :file-obj="fileList" :file-num="0" />
        </el-form-item>
      </div>
      <div class="maintitle" style="margin-top:40px;">
        <div class="stepnumber" style="opacity:0.7;">2</div>
        <div class="title">竣工测绘机构名称</div>
      </div>
      <div class="formcontent">
        <el-form-item v-if="form.pickMethod!=4" label="选择单位/公司" label-width="153px">
          <el-select filterable :disabled="disabled" v-model="form.companyId" placeholder="请选择">
            <el-option
              v-for="item in institutionsList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.pickMethod!=4" :label="notifyFileName" label-width="153px">
          <el-upload
            class="upload-demo"
            :action="uploadUrl"
            v-if="!disabled"
            accept=".pdf"
            :disabled="disabled"
            :file-list="fileNotice"
            :on-success="gerNoticeList"
            :on-remove="removeNoticeList"
          >
            <el-button v-if="!disabled" size="small">上传</el-button>
            <!-- <el-button @click="seePdf(1)" v-if="disabled" size="small">查看</el-button> -->
          </el-upload>
          <seeFujianForList v-if="disabled" @openPdf="seePdf" :file-obj="fileNotice" :file-num="0" />
        </el-form-item>
      </div>
      <div class="maintitle" style="margin-top:60px;">
        <div class="stepnumber" style="opacity:0.7;">3</div>
        <div class="title">合同信息</div>
      </div>
      <div class="formcontent">
        <el-form-item label="已签订的合同" label-width="148px">
          <el-upload
            class="upload-demo"
            :action="uploadUrl"
            v-if="!disabled"
            accept=".pdf"
            :disabled="disabled"
            :file-list="filecontract"
            :on-success="getcontractList"
            :on-remove="removeContractList"
          >
            <el-button v-if="!disabled" size="small">上传</el-button>
            <!-- <el-button @click="seePdf(2)" v-if="disabled" size="small">查看</el-button> -->
          </el-upload>
          <seeFujianForList
            v-if="disabled"
            @openPdf="seePdf"
            :file-obj="filecontract"
            :file-num="0"
          />
        </el-form-item>
      </div>
      <div class="savebtn">
        <el-form-item>
          <el-button v-if="!disabled" type="primary" @click="onSubmit">保存</el-button>
          <el-button v-if="disabled" @click="back">返回</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-dialog :close-on-click-modal="false" :title="pdfName" :visible.sync="pdfFlag" width="50%">
      <div class="pdfBox">
        <iframe :src="$https + pdfUrl" style="width:100%;height:600px;" frameborder="0"></iframe>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import process from "../public/process";
import {
  getCompanyList,
  getproductDetail,
  getInstitutionsList,
  saveUnitSelection
} from "@/api/project1/completedMapping/countyUnit";
import config from "@/config/index";
import seeFujianForList from "@/components/seeFujianForList";
export default {
  components: {
    seeFujianForList
  },
  data() {
    return {
      form: {
        projectId: localStorage.getItem("projectId"),
        pickMethod: "",
        pickFileId: "",
        companyId: "",
        notifyFileId: "",
        contractFileId: ""
      },
      fileList: [],
      fileNotice: [],
      filecontract: [],
      institutionsList: [],
      companyList: [],
      pickFileName: "招标文件",
      notifyFileName: "中标通知书",
      uploadUrl: config.uploadUrl, //上传接口
      projectId: localStorage.getItem("projectId") || "",
      disabled: true,
      pdfUrl: "",
      pdfFlag: false,
      pdfName: ""
    };
  },

  created() {
    if (this.$route.name === localStorage.getItem("key")) {
      this.disabled = false;
    }
    if (localStorage.getItem("lookStatus") == 2) {
      this.disabled = true;
    }
    this.getCompanyList();
    this.getInstitutionsList();
  },
  methods: {
    //查看当前PDF
    seePdf(num) {
      this.pdfFlag = true;
      switch (num) {
        case 0:
          this.pdfUrl = this.fileList[0].url;
          this.pdfName = this.fileList[0].name;
          break;
        case 1:
          this.pdfUrl = this.fileNotice[0].url;
          this.pdfName = this.fileNotice[0].name;
          break;
        case 2:
          this.pdfUrl = this.filecontract[0].url;
          this.pdfName = this.filecontract[0].name;
          break;
      }
    },
    getCompanyList() {
      var that = this;
      getCompanyList().then(res => {
        that.companyList = res.data;
        that.getproductDetail();
      });
    },
    getproductDetail() {
      var that = this;
      getproductDetail({
        projectId: this.form.projectId
      }).then(res => {
        if (res.data) {
          that.form.pickMethod = res.data.pickMethod;
          that.form.companyId = res.data.companyId;
          if (res.data.pickFile) {
            that.form.pickFileId = res.data.pickFile.id;
            var pickFileData = {
              name: res.data.pickFile.fileName,
              url: res.data.pickFile.filePath
            };
            that.fileList.push(pickFileData);
          }
          if (res.data.notifyFile) {
            that.form.notifyFileId = res.data.notifyFile.id;
            var notifyFileData = {
              name: res.data.notifyFile.fileName,
              url: res.data.notifyFile.filePath
            };
            that.fileNotice.push(notifyFileData);
          }
          if (res.data.contractFile) {
            that.form.contractFileId = res.data.contractFile.id;
            var contractFileData = {
              name: res.data.contractFile.fileName,
              url: res.data.contractFile.filePath
            };
            that.filecontract.push(contractFileData);
          }
          that.gettablename(that.form.pickMethod);
        }
      });
    },
    getInstitutionsList() {
      var that = this;
      getInstitutionsList().then(res => {
        that.institutionsList = res.data;
      });
    },
    gettablename(pickMethodId) {
      var that = this;
      this.companyList.map(item => {
        if (item.pickMethod === pickMethodId) {
          that.pickFileName = item.pickDocument;
          that.notifyFileName = item.notifyDocument;
        }
      });
    },
    getstyles() {
      this.gettablename(this.form.pickMethod);
    },
    gerFileList(res, file) {
      this.fileList = [];
      var files = {
        name: res.data.fileName,
        url: res.data.filePath
      };
      this.form.pickFileId = res.data.id;
      this.fileList.push(files);
    },
    gerNoticeList(res, file) {
      this.fileNotice = [];
      var files = {
        name: res.data.fileName,
        url: res.data.filePath
      };
      this.form.notifyFileId = res.data.id;
      this.fileNotice.push(files);
    },
    getcontractList(res, file) {
      this.filecontract = [];
      var files = {
        name: res.data.fileName,
        url: res.data.filePath
      };
      this.form.contractFileId = res.data.id;
      this.filecontract.push(files);
    },
    removeFileList(file, fileList) {
      this.fileList = [];
      this.form.pickFileId = "";
    },
    removeNoticeList(file, fileList) {
      this.fileNotice = [];
      this.form.notifyFileId = "";
    },
    removeContractList() {
      this.filecontract = [];
      this.form.contractFileId = "";
    },
    onSubmit() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      // console.log(this.form)
      if (this.form.pickMethod == 4) {
        this.form.pickFileId = "";
        this.form.notifyFileId = "";
      }
      var that = this;
      saveUnitSelection(this.form)
        .then(res => {
          loading.close();
          this.$message({
            message: "提交成功",
            type: "success"
          });
          this.$router.push(
            "/projectManagement/projectImplementation/dealwith"
          );
          // this.$router.push('/completedMapping/countyInput?projectId='+that.form.projectId);
        })
        .catch(err => {
          loading.close();
        });
    },
    back() {
      this.$router.push("/projectManagement/projectImplementation/dealwith");
    }
  }
};
</script>

<style lang="scss" scoped>
.countyUnit {
  padding: 20px;
  min-height: calc(100vh - 154px);
  background-color: #fff;
  .maintitle {
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    align-items: center;
    .stepnumber {
      width: 32px;
      height: 32px;
      background: #2e8ae6;
      text-align: center;
      line-height: 32px;
      border-radius: 100px;
      font-size: 18px;
      font-family: Helvetica;
      color: #ffffff;
    }
    .title {
      -webkit-box-flex: 1;
      -webkit-flex: 1;
      flex: 1;
      margin-left: 20px;
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #333333;
    }
  }

  .formcontent {
    margin-top: 30px;
  }
  .savebtn {
    margin-top: 60px;
    /deep/ .el-form-item__content {
      margin-left: 45px !important;
    }
  }
}
/deep/ .el-form--inline .el-form-item__label {
  color: #a1a1a1;
}
</style>